﻿using System;
using System.Collections.Generic;
using System.Text;
using IBLL.Controller;
using IBLL.View;
using Model;
using Common.Logging;
using System.IO;
using Utility;
using BLL.Business;
using AppToolKit.Core.Context.Spring;

namespace BLL.Controller
{
    public abstract class EditControllerTemplate : WinFormControllerBase, IEditCotrollerTemplate
    {
        protected ILog log = LogManager.GetLogger(LogDefault.AppInfo);

        protected  IEditView DeriveView
        {
            get { return (IEditView)View; }
        }
        private EntityState state_;

        [AutoWiring]
        public EntityBusiness EntityBusinessHolder { get; set; }

        public abstract string XmlName { get; }

        public  DataTrans Data{get;set;}
        /// <summary>
        /// 主建
        /// </summary>
        // protected object PrimaryKey
        //{
        //    get { return key_; }
        //    set { key_ = value; }
        //}

        //private object key_ = null;

        public EditControllerTemplate()
        { }
        public EditControllerTemplate(object key)
        {
            PrimaryKey = key;
        }
        protected override void AssignEvent()
        {
            DeriveView.ExitAction += ExitAction;
            DeriveView.CreateAction += CreateAction;
            DeriveView.DeleteAction += DeleteAction;
            DeriveView.SaveAction += SaveAction;
            
           
        }
        public override void OnInitialization()
        {
            base.OnInitialization();

            Load(PrimaryKey);
        }
        bool IsConfim(string message)
        {
           return DeriveView.IsConfim(message);
        }
       

        private void RefreshUI(object sender, string id)
        {
            Refresh(sender, id);
        }

        
        void SaveAction(object sender, EventArgs e)
        {
            Save();
        }

        void DeleteAction(object sender, EventArgs e)
        {
            Delete();
        }

        void CreateAction(object sender, EventArgs e)
        {
            PrimaryKey = null;
            DoCreate();
        }

        void ExitAction(object sender, EventArgs e)
        {
            Close();
        }
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="key"></param>
        private void DoLoad(object key)
        {

            SetState(EntityState.Edit);
            OnLoad(key);
            //防止数据加载失败
            if (Data.IsNull)
            {
                //throw new RecordControllerException(new List<string> { "加载数据失败" });
            }

            DeriveView.OnSetDataToView(state_, Data);
            AdjustView();
        }
        public void Load(object key)
        {
            if (key == null)
            {
                DoCreate();


            }
            else
            {

                DoLoad(key);

            }

        }
        /// <summary>
        /// 从界面获取数据后验证
        /// </summary>
        /// <returns></returns>
        public virtual bool OnDataVaild(EntityState state, out string error)
        {
            error = string.Empty;
            return true;
        }
        public void Save()
        {
            DeriveView.OnGetFromView(state_, Data);
            //界面附加处理
            DeriveView.BeforeSaveAction(Data);
            string Error = string.Empty;
            if (!OnDataVaild(state_, out Error))
            {
                DeriveView.ShowError(Error);

            }
            try
            {
                if (state_ == EntityState.New)
                {
                    OnSave();
                    //更新主键
                    PrimaryKey = Data.GetPrimaryKey();

                    SetState(EntityState.Edit);

                }
                else
                {
                    OnUpdate();

                }

               
                AdjustView();
            }
            catch (Exception ex)
            {
                 log.Debug(ex.Message);
                throw;
                //SmartMessageListForm.Error(ex.Message);

            }



        }

        private void DoCreate()
        {
            SetState(EntityState.New);
            OnCreate();
            
            DeriveView.OnSetDataToView(state_, Data);
            AdjustView();

        }
        /// <summary>
        /// 保存处理
        /// </summary>
        /// <returns></returns>
        public void OnSave()
        {
            EntityBusinessHolder.Save(Data);
        }
        

        public  void OnUpdate()
        {
            EntityBusinessHolder.Update(Data);
        }
        /// <summary>
        /// 加载处理
        /// </summary>
        public void OnLoad(object key)
        {
           Data= EntityBusinessHolder.Load(key , XmlName) as DataTrans;
        }
        /// <summary>
        /// 新建处理
        /// </summary>
        /// <returns></returns>
        public void OnCreate()
        {
           Data= EntityBusinessHolder.Create(XmlName);
        }


        public void OnDelete()
        {
            EntityBusinessHolder.Delete(Data.GetPrimaryKey(), XmlName);
        }
        public void Delete()
        {
            //提示
            if (IsConfim("你确定要删除!"))
            {
                try
                {
                    OnDelete();

                    ExitAction(null, null);

                }
                catch
                {
                }
            }
             


        }
        /// <summary>
        /// 在加载，保存之后调整界面
        /// DeriveView.OnAdjustUI
        /// </summary>
        protected virtual void AdjustView()
        {
            DeriveView.OnAdjustUI(state_, Data);
        }

        private void SetState(EntityState state)
        {
            state_ = state;
        }


       
    }
}
